package com.qf.rbac2202.shiro;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(){

        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
        //配置直接放行的请求
        chainDefinition.addPathDefinition("/register.html","anon");
        chainDefinition.addPathDefinition("/swagger-ui.html","anon");
        chainDefinition.addPathDefinition("/doc.html","anon");
        chainDefinition.addPathDefinition("/webjars/**","anon");
        chainDefinition.addPathDefinition("/swagger-resources/**","anon");
        chainDefinition.addPathDefinition("/csrf","anon");
        chainDefinition.addPathDefinition("/v2/**","anon");
//        chainDefinition.addPathDefinition("/statics/**","anon");
//        chainDefinition.addPathDefinition("/sys/login","anon");
//        chainDefinition.addPathDefinition("/login.html","anon");
//        chainDefinition.addPathDefinition("/captcha.jpg","anon");

        //需要登录后才能访问的url请求
//       chainDefinition.addPathDefinition("/**","authc");
        return chainDefinition;
    }

    @Bean
    public Realm customRealm(){

        CustomRealm customRealm = new CustomRealm();

        //密码算吗设置
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName(ShiroConstons.HASH_ALGORITHM);
        hashedCredentialsMatcher.setHashIterations(ShiroConstons.HASH_TINES);
        customRealm.setCredentialsMatcher(hashedCredentialsMatcher);


        return customRealm;
    }
}
